package com.example.celldian.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBWorld extends SQLiteOpenHelper {

	public DBWorld(Context activity) {
		super(activity, "Celldian.db", null, 1);
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		String query = "CREATE TABLE world ( charname TEXT PRIMARY KEY, mapname TEXT, " +
					   "progress TEXT, missions TEXT)";
		
		db.execSQL(query);	
		
		Log.i("DATABASE DBWorld:","TABLE world SUCCESSFULY CREATED!");
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String query = "DROP TABLE IF EXISTS world";
		
		db.execSQL(query);
		onCreate(db);				
		
		Log.i("DATABASE DBWorld:","UPDATE SUCCESSFUL!");
	}
	
	public void insertMap(HashMap<String ,String> MapValues) {
			
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", MapValues.get("charname"));
		values.put("mapname", MapValues.get("mapname"));
		values.put("progress", MapValues.get("progress"));
		values.put("missions", MapValues.get("missions"));
		
		db.insert("world", null, values);
		db.close();
		
		Log.i("DATABASE DBWorld:","MAP INSERT WAS SUCCESSFUL!");
	}
	
	public int updateMap(HashMap<String ,String> MapValues) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put("charname", MapValues.get("charname"));
		values.put("mapname", MapValues.get("mapname"));
		values.put("progress", MapValues.get("progress"));
		values.put("missions", MapValues.get("missions"));
		
		Log.i("DATABASE DBWorld:","MAP UPDATE WAS SUCCESSFUL!");
		
		return db.update("world", values, "charname" + "?=", new String[] {MapValues.get("charname")});
	}
	
	public void deleteMap(String charname) {
		
		SQLiteDatabase db = this.getWritableDatabase();
		
		String query = "DELETE FROM world WHERE charname='" + charname + "'";
		
		db.execSQL(query);
		
		Log.i("DATABASE DBWorld:","MAP DELETE WAS SUCCESSFUL!");
	}
	
	public ArrayList<HashMap<String, String>> getWorld() {
		
		ArrayList<HashMap<String, String>> MapList = new ArrayList<HashMap<String, String>>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM world";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				HashMap<String, String> map = new HashMap<String, String>();
				
				map.put("charname", cursor.getColumnName(0));
				map.put("mapname", cursor.getColumnName(1));
				map.put("progress", cursor.getColumnName(2));
				map.put("missions", cursor.getColumnName(3));
				
				MapList.add(map);
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBWorld:","WORLD RETURN WAS SUCCESSFUL!");
		
		return MapList;
	}
	
	public HashMap<String, String> getMap(String charname){
		
		HashMap<String, String> Maps = new HashMap<String, String>();
		SQLiteDatabase db = this.getReadableDatabase();
		
		String query = "SELECT * FROM world WHERE charname='" + charname + "'";
		
		Cursor cursor = db.rawQuery(query, null);
		
		if (cursor.moveToFirst()) {
			
			do {
				Maps.put("charname", cursor.getColumnName(0));
				Maps.put("mapname", cursor.getColumnName(1));
				Maps.put("progress", cursor.getColumnName(2));
				Maps.put("missions", cursor.getColumnName(3));
			} while(cursor.moveToNext());
		}
		
		Log.i("DATABASE DBWorld:","MAP RETURN WAS SUCCESSFUL!");
		
		return Maps;
	}
}